let app = new Vue({
	el: '#app',
	data:{
		add_name:'',
		add_unitPrice: '',
		add_company: '',
		add_number: '',
		add_forecast: '',
		update_name: '',
		update_unitPrice: '',
		update_company: '',
		update_number: '',
		update_forecast: '',
		update_id: '',
		query_name: '',
		query_state: '',
		ackAll: false,
		list: [],
		size: 10,
		curr: 1,
		total: '',
		arr: [],
	},
	created: function(){
		this.getList();
	},
	methods:{
		getList: function(){
			axios.get('http://chonghekj.com/crud/index.php/Home/Index/getItemList', {
				params: {
					pageSize: this.size,
					pageNumber: this.curr,
					item_name: this.query_name,
					item_state: this.query_state,
				}
			})
			.then((res)=>{
				// for(let i=0; i<res.data.rows.length; i++){
					this.list = res.data.rows;
					this.total = res.data.total;
					// this.item_name = res.data.rows[i].item_name
				// }
				console.log(res.data.rows);
			})
		},
		submit: function(){
			axios.post('http://chonghekj.com/crud/index.php/Home/Index/addItem',Qs.stringify({
					item_name: this.add_name,
					item_price: this.add_unitPrice,
					item_format: this.add_company,
					item_total: this.add_number,
					item_inventory: this.add_forecast,
			}))
			.then((res)=>{
				console.log(res);
				this.getList();
				this.$refs.add.style.top = '-530px';
				this.$refs.add.style.opacity = '0';
				// this.list = res.data.rows;
			})
		},
		addItm: function(){
			if(this.arr.length == 0){
				alert('请选中要修改的项！')
			}if(this.arr.length > 1){
				alert('请勿多选！')
			}if(this.arr.length == 1){
				this.$refs.modify.style.top = '50px';
				this.$refs.modify.style.opacity = '1';
			}
		},
		close1: function(){
			this.$refs.modify.style.top = '-530px';
			this.$refs.modify.style.opacity = '0';
		},
		select: function(val){
			// console.log(val);
			this.update_id = val.item_id,
			this.update_name = val.item_name,
			this.update_unitPrice = val.item_price,
			this.update_company = val.item_format,
			this.update_number = val.item_total,
			this.update_forecast = val.item_inventory
		},
		submit1: function(){
			axios.post('http://chonghekj.com/crud/index.php/Home/Index/editItem',Qs.stringify({
					item_name: this.update_name,
					item_price: this.update_unitPrice,
					item_format: this.update_company,
					item_total: this.update_number,
					item_inventory: this.update_forecast,
					item_id: this.update_id
			}))
			.then((res)=>{
				// console.log(res);
				this.getList();
				this.$refs.modify.style.top = '-530px';
				this.$refs.modify.style.opacity = '0';
			})
		},
		deleteItem: function(){
			if(this.arr.length == 0){
				alert('请选中要删除项！');
			}else{
				axios.post('http://chonghekj.com/crud/index.php/Home/Index/deleteItem',Qs.stringify({
					ids: this.arr
				}))
				.then((res)=>{
					console.log(res);
					this.getList();
					this.arr = [];
				})
			}
		},
		query: function(){
			this.getList();
			this.query_name = '';
		},
		changeSize: function() {
			this.getList();
			this.curr = 1;
		},
		changeCurr: function(curr){
			// console.log(this.curr)
			this.curr = curr;
			this.getList();
		},
		display: function(){
			// console.log(11)
			this.$refs.add.style.top = '50px';
			this.$refs.add.style.opacity = '1';
		},
		close: function(){
			this.$refs.add.style.top = '-530px';
			this.$refs.add.style.opacity = '0';
		},
		changeAll: function(){
			// console.log(this.ackAll)
			if(this.ackAll){
				this.arr = this.list.map(function(val){
					return val.item_id;
				})
			}else{
				this.arr = []
			}
		},
		changeOne: function(){
			if(this.arr.length == this.list.length){
				this.ackAll = true
			}else{
				this.ackAll = false
			}
		}
	},
	computed:{
		pagenum: function(){
			return Math.ceil(this.total / this.size)
		}
	}
})